import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

/*
  Generated class for the StorageProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class StorageProvider {
  public localStorage:any;

  constructor(public http: HttpClient) {
    if (!localStorage) {
            throw new Error('Current browser does not support Local Storage');
        }
        this.localStorage = localStorage;
  }

  public set(key:string, value:string):void {
    this.localStorage[key] = value;
  }

  public get(key:string):string {
      return this.localStorage[key] || false;
  }

  public setObject(key:string, value:any):void {
      this.localStorage[key] = JSON.stringify(value);
  }

  public getObject(key:string):any {
      let str = this.localStorage[key];
      if(str===undefined||str==='undefined') str = '{}';
      return JSON.parse(str);
  }

  public remove(key:string):any {
      this.localStorage.removeItem(key);
  }
}
